package com.digitalxpert.jargle.client;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.swing.JTextArea;
public class JGFramePather {
	/*
	 * private JGApplication app; private Collection<JGLibrary> libs = new
	 * ArrayList<JGLibrary>(); private Collection<JGPackage> pkgs; private
	 * Collection<JGClass> classes;
	 */
	private final String newline = "\n";
	private JTextArea log = null;
	public JGFramePather(JTextArea log) {
		this.log = log;
	}
	public void extractFiles(File selDir) {
		log.append("Opening: " + selDir.getName() + "." + newline);

		// app.setAppName(selDir.getName());
		File[] files = selDir.listFiles(new JarFileFilter());
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				extractFiles(files[i]);
			} else {
				log.append("In this directories: " + files[i].getName() + "." + newline);
				extractClass(files[i]);
			}
		}
	}
	public void extractClass(File file) {
		/*
		 * JGLibrary lib = new JGLibrary(); JGClass cl = new JGClass();
		 * JGPackage pkg = new JGPackage(); lib.setLibName(file.getName());
		 */
		int count = 0;
		try {
			JarFile jar = new JarFile(file);
			Enumeration<JarEntry> entries = jar.entries();
			String path = null;
			String name = null;
			while (entries.hasMoreElements()) {
				path = entries.nextElement().getName();
				if (path.endsWith(".class")) {
					name = path.substring(path.lastIndexOf("/") + 1);
					// cl.setClassName(name);
					// classes.add(cl);
					log.append("	Class in this jar: " + name + newline);
					int lastIndexOfSlash = path.lastIndexOf("/");
					if(lastIndexOfSlash!=-1){
						path = path.substring(0, lastIndexOfSlash);
					}
					path = path.replace('/', '.');
					// pkg.setPkgName(path);
					// pkg.setClasses(classes);
					log.append("	With path: " + path + newline);
					count++;
				}
			}
			
			log.append("Count: " + count);
		} catch (IOException ie) {
			System.out.println(ie.toString());
		}
		// libs.add(lib);
	}
	private class JarFileFilter implements java.io.FileFilter {
		public boolean accept(File f) {
			if (f.isDirectory())
				return true;
			String name = f.getName().toLowerCase();
			return name.endsWith("jar");
		}
	}
}
